var baseUrl='../'+T.p('baseUrl')+'/';
$(function () {
    $("#jqGrid").jqGrid({
        url: baseUrl+'list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: "id", width: 45, key: true,hidden:true },
			{ label: '操作', name: '', index: "", width: 45,formatter:function(cellvalue, options, rowObject){
				var deleteString='<a onclick=\'vm.del("'+rowObject.id+'")\'>删除</a>';
				var update='<a onclick=\'vm.update("'+rowObject.id+'")\'>&nbsp编辑</a>';
				var rename='<a onclick=\'vm.rename("'+rowObject.id+'","'+rowObject.name+'")\'>&nbsp重命名</a>';
				var upzip='<a onclick=\'vm.unzip("'+rowObject.id+'")\'>&nbsp解压</a>';
				if(rowObject.type=="text"){
					deleteString+=update;
				}
				if(rowObject.type=="zip"){
					deleteString+=upzip;
				}
				if(rowObject.type=="directory"&&(rowObject.name!='..')){
					deleteString+=rename;
				}
				return deleteString;
				
			}},
			{ label: '名称', name: 'name', index: "name", width: 100,formatter:function(cellvalue, options, rowObject){
				if(rowObject.type=="directory"){
					return '<a onclick=\'vm.toDirectory("'+rowObject.id+'")\'>'+cellvalue+'</a>';
				}
				if(rowObject.type=="text"){
					return '<a onclick=\'vm.update("'+rowObject.id+'")\'>'+cellvalue+'</a>';
				}
				return cellvalue;
				
			}},
			{ label: '类型', name: 'type', index: "type", width: 75 },
			{ label: '大小', name: 'lengthKB', index: "lengthKB",width: 20 },
			{ label: '修改时间', name: 'lastModified', index: "lastModified", width: 80}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        //pager: "#jqGridPager",
        jsonReader : {
            root: "list",
        },
        /*prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },*/
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    new AjaxUpload('#upload', {
        action: baseUrl+'upload',
        name: 'file',
        autoSubmit:true,
        data:vm.q,
        responseType:"json",
        onSubmit:function(file, extension){
        },
        onComplete : function(file, r){
            if(r.code == 0){
                vm.reload();
            }else{
                alert(r.msg);
            }
        }
    });
    new AjaxUpload('#zipUpload', {
    	action: baseUrl+'zipUpload',
    	name: 'file',
    	autoSubmit:true,
    	data:vm.q,
    	responseType:"json",
    	onSubmit:function(file, extension){
    	},
    	onComplete : function(file, r){
    		if(r.code == 0){
    			vm.reload();
    		}else{
    			alert(r.msg);
    		}
    	}
    });
});

var setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "id",
			pIdKey: "parent",
			rootPId: ""
		},
		key: {
			url:"nourl",
			name: "baseName"
		}
	},
	async: {
		enable: true,
		type: "post",
		//contentType: "application/json",
		dataType :"text",
		url: baseUrl+"leftTree",
		dataFilter: function (treeId, parentNode, responseData) {
			var dates=responseData.list;
			for(var i =0; i < dates.length; i++) {
				dates[i].isParent =true ;
		      }
			return dates;
		},
		autoParam: ["id=parentId"]
	},
	check:{
		enable:false,
		nocheckInherit:true
	},
	callback: {
		onClick:function(event, treeId, treeNode) {
		    //alert(treeNode.tId + ", " + treeNode.name);
			vm.q.parentId=treeNode.id;
			vm.reload();
		},
	}
};

var ztree;
	
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			searchName: null,
			parentId:''
		},
		showList: true,
		title:null,
		fileinfo:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fileinfo = {};
			/*vm.getMenuTree(null);*/
		},
		update: function (fileId) {
			//var fileId = getSelectedRow();
			if(fileId == null){
				return ;
			}
			
			vm.showList = false;
            vm.title = "修改";
            this.getFile(fileId);
		},
		rename: function (fileId,fileName) {
			//var fileId = getSelectedRow();
			if(fileId == null){
				return ;
			}
			var fileinfo={baseName:fileName};
			vm.fileinfo=fileinfo;
			vm.index=layer.open({
				title:"重命名",
                type: 1,
				area:  ['300px', '200px'],
                shade:[0.5,'#000'],
				content: $('#filename'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
				btn: ['取消', '确认'],
				btn2: function (indexP, layero) {
					var url=baseUrl+"rename",param={};
					param.id=fileId;
					param.name=vm.fileinfo.baseName;
					$.ajax({
						type: "POST",
					    url: url,
		                contentType: "application/json",
					    data: JSON.stringify(param),
					    success: function(r){
					    	if(r.code === 0){
					    			layer.close(indexP);
								alert('操作成功', function(index){
									vm.reload();
								});
							}else{
								alert(r.msg);
							}
						}
					});
					return false;
		        },
            });
		
		},
		del: function (id) {
			var url=baseUrl+"delete",param={};
			if (id) {
				param.ids=id;
			}
			if (!param.ids) {
				var fileIds = getSelectedRows();
				if(fileIds == null){
					return ;
				}
				param.ids=fileIds.join();
			}
			$.ajax({
				type: "POST",
			    url: url,
                contentType: "application/json",
			    data: JSON.stringify(param),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		zip: function (event) {
			var url=baseUrl+"zip",param={};
			var fileIds = getSelectedRows();
			if(fileIds == null){
				return ;
			}
			param.ids=fileIds.join();
			$.ajax({
				type: "POST",
			    url: url,
                contentType: "application/json",
			    data: JSON.stringify(param),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		mkdir: function (event) {
			var url=baseUrl+"mkdir",param={};
			param.parentId=vm.q.parentId;
			param.dir="新建文件夹";
			$.ajax({
				type: "POST",
			    url: url,
                contentType: "application/json",
			    data: JSON.stringify(param),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		zipDownload: function (event) {
			var url=baseUrl+"zipDownload",param={};
			var fileIds = getSelectedRows();
			if(fileIds == null){
				return ;
			}
			vm.reload();
			location.href = url+"?id=" + fileIds.join();
		},
		unzip: function (fileIds) {
			var url=baseUrl+"unzip",param={};
//			var fileIds = getSelectedRows();
			if(fileIds == null){
				return ;
			}
			param.ids=fileIds;
			$.ajax({
				type: "POST",
			    url: url,
                contentType: "application/json",
			    data: JSON.stringify(param),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		zipUpload: function (event) {
			var url=baseUrl+"zipUpload",param={};
			var fileIds = getSelectedRows();
			if(fileIds == null){
				return ;
			}
			param.ids=fileIds.join();
			$.ajax({
				type: "POST",
			    url: url,
                contentType: "application/json",
			    data: JSON.stringify(param),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getFile: function(fileId){
			 $.get(baseUrl+"info?id="+fileId+"&parentId="+vm.q.parentId, function(r){
	            	vm.fileinfo = r.fileinfo;
	    		});
		},
		saveOrUpdate: function (event) {
			var url,param={};
			if (vm.fileinfo.id == null) {
				url =  "save";
				param.parentId=vm.q.parentId;
				param.name=vm.fileinfo.baseName+"."+vm.fileinfo.extension;
				param.text=vm.fileinfo.text;
			}else{
				url =  "update";
				param.parentId=vm.q.parentId;
				param.name=vm.fileinfo.baseName+"."+vm.fileinfo.extension;
				param.origName=vm.fileinfo.name;
				param.text=vm.fileinfo.text.toBase64();
			}
			url=baseUrl+url;
			$.ajax({
				type: "POST",
			    url: url,
                contentType: "application/json",
			    data: JSON.stringify(param),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		toDirectory: function (id) {
			vm.q.parentId=id;
			vm.reload();
		},
		toDirectory: function (id) {
			vm.q.parentId=id;
			vm.reload();
		},
	    reload: function (event) {
	    	 	vm.showList = true;
	    	    $("#jqGrid").jqGrid('setGridParam',{ 
	                postData:vm.q,
	            }).trigger("reloadGrid");
		}
	},
	mounted: function () {
		ztree = $.fn.zTree.init($("#menuTree"), setting,null);
    	//加载菜单树
//		$.get("../workspaceWebFile/leftTree", function(r){
//			ztree = $.fn.zTree.init($("#menuTree"), setting, r.list);
//			//展开所有节点
//			//ztree.expandAll(true);
//			
//			/*if(roleId != null){
//				vm.getRole(roleId);
//			}*/
//		});
	},
});